﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.Utility;

namespace Lead2012.Repository
{
    public class LeadCalendarItemRepository
    {
        // Create an instance of entity
        Lead2012Entities entities = new Lead2012Entities();

        public List<CalendarInfoEx> GetAllCalendarBy(int userId, DateTime startDate)
        {
            List<CalendarInfoEx> list = new List<CalendarInfoEx>();

            var items = (from c in entities.CalendarItems
                         where c.UserId == userId
                         select new
                         {
                             LeadId = c.LeadId,
                             Title = c.Title,
                             StartDate = c.StartDate
                         }).ToList();

            // Get current date
            if (items != null && items.Count > 0)
            {
                items = items.Where(c => c.StartDate.ToShortDateString().Equals(startDate.ToShortDateString())).ToList();
            }

            // Get all field system
            var listFieldSystem = new  FieldSystemRepository().GetAll();

            foreach (var item in items)
            {
                CalendarInfoEx model = new CalendarInfoEx();
                model.Id = (item.LeadId == null) ? 0 : int.Parse(item.LeadId.ToString());
                model.LeadName = GetLeadName(listFieldSystem, model.Id);
                model.StartDate = item.StartDate;
                model.Title = item.Title;
                list.Add(model);
            }

            return list;
        }

        public CalendarReminderItem GetCaledarReminder(int userId)
        {
            CalendarReminderItem model = new CalendarReminderItem();

            var list = (from c in entities.CalendarItems
                        where c.UserId == userId
                           && c.IsCompleted == false
                           && c.ReminderMins > 0
                        select new
                        {
                            Id = c.ItemId,
                            LeadId = c.LeadId,
                            Title = c.Title,
                            StartDate = c.StartDate
                        }).ToList();

            var item = list.Where(c => c.StartDate.Day == DateTime.Now.Day
                            && c.StartDate.Month == DateTime.Now.Month
                           && c.StartDate.Year == DateTime.Now.Year
                           && (c.StartDate - DateTime.Now).Minutes >= 15
                           && (c.StartDate - DateTime.Now).Minutes <= 40).FirstOrDefault();

            // Get all field system
            var listFieldSystem = new FieldSystemRepository().GetAll();

            if (item != null)
            {
                model.Id = item.Id;
                model.Title = item.Title;
                model.StartDate = item.StartDate.ToString("dd-MM-yyyy");
                model.StartTime = item.StartDate.ToString("hh:mm tt");
                model.LeadName = GetLeadName(listFieldSystem, int.Parse(item.LeadId.ToString()));
            }

            return model;
        }

        public List<CalendarInfoEx> GetAllCalendarBy(DateTime startDate)
        {
            List<CalendarInfoEx> list = new List<CalendarInfoEx>();

            var items = (from c in entities.CalendarItems
                         select new
                         {
                             LeadId = c.LeadId,
                             Title = c.Title,
                             StartDate = c.StartDate
                         }).ToList();

            // Get current date
            if (items != null && items.Count > 0)
            {
                items = items.Where(c => c.StartDate.ToShortDateString().Equals(startDate.ToShortDateString())).ToList();
            }

            // Get all field system
            var listFieldSystem = new FieldSystemRepository().GetAll();

            foreach (var item in items)
            {
                CalendarInfoEx model = new CalendarInfoEx();
                model.Id = (item.LeadId == null) ? 0 : int.Parse(item.LeadId.ToString());
                model.LeadName = GetLeadName(listFieldSystem, model.Id);
                model.StartDate = item.StartDate;
                model.Title = item.Title;
                list.Add(model);
            }

            return list;
        }

        private string GetLeadName(List<FieldSystem> listFieldSystem, int leadId)
        {
            string leadName = string.Empty;

            // Get lead information
            var leads = new LeadWarehouseManagementRepository().GetLeadWarehouseInformation(leadId);

            // Get all fields build to LeadName
            var fieldLeads = listFieldSystem.Where(c => c.TextColumnName.Equals("Name", StringComparison.OrdinalIgnoreCase)).ToList();
            if(fieldLeads!=null && fieldLeads.Count>0)
            {
                for (int i = 0; i < fieldLeads.Count;i++ )
                {
                    string fieldName = string.Format("f{0}", fieldLeads[i].FieldId);
                    var item = leads.Where(c=>c.ColumnName.Equals(fieldName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                    if (i == 0)
                        leadName += string.Format("{0}", item.ColumnValue);
                    else
                        leadName += string.Format(" {0}", item.ColumnValue);

                }
            }

            return leadName;
        }
    }
}